package cc.vv.btpartyjob.homepage.activity

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.homepage.R
import cc.vv.btpartyjob.homepage.adapter.MineVolunteerProjectAdapter
import cc.vv.btpartyjob.homepage.delegate.MineVolunteerProjectActivityDelegate
import cc.vv.btpartyjob.homepage.moudel.resp.MineVolunteerProjectResponseObj
import cc.vv.btpartyjob.homepage.server.HomeHttpServer
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.moudel.bean.VolProBean
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 我的志愿项目项目
 * Created by Chen.qi on 2018/10/24.
 */
class MineVolunteerProjectActivity : BaseActivityMVP<MineVolunteerProjectActivityDelegate>(), BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener {

    /**
     * 适配器
     */
    private var mAdapter: MineVolunteerProjectAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<VolProBean>? = null

    override fun initData() {
        mDataList = ArrayList()
        viewDelegate?.setTopBarTitle("我的志愿项目")

        if (mAdapter == null) {
            mAdapter = MineVolunteerProjectAdapter(R.layout.item_mine_volunteer_project)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
        HomeHttpServer.mineVolunteerProjectList(true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
    }

    override fun getDelegateClass(): Class<MineVolunteerProjectActivityDelegate> {
        return MineVolunteerProjectActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is MineVolunteerProjectResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                mDataList?.clear()
                if (null != obj.data) {
                    mDataList!!.addAll(obj.data)
                }
                viewDelegate?.setAdapterData(mAdapter, mDataList)
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        viewDelegate?.stopRefreshing()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        viewDelegate?.stopRefreshing()
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        JniStartActivity.startIntentWebActivity(this, H5UrlEnum.志愿项目详情, mDataList?.get(position)?.id,
                "详情")
    }

    override fun onRefresh() {
        HomeHttpServer.mineVolunteerProjectList(false)
    }

}